/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.staticc;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import finbudgettable.client.ui.grid.cell.HasMultilineStyle;

public class StaticCell extends ClickableTextCell implements HasMultilineStyle {

    interface Template extends SafeHtmlTemplates {
        @Template("<div class=\"{0}\">{1}</div>")
        SafeHtml div(String className, SafeHtml text);
    }

    interface Resources extends ClientBundle {

        interface Style extends CssResource {
            @ClassName("b-static-cell__text")
            String bStaticCell__text();

            @ClassName("b-static-cell")
            String bStaticCell();

            @ClassName("b-static-cell__multiline_text")
            String bStaticCell__multiline_text();
        }

        @Source("StaticCell.css")
        Style style();
    }

    private static final Template TEMPLATE = GWT.create(Template.class);
    static final Resources RESOURCES = GWT.create(Resources.class);

    static {
        RESOURCES.style().ensureInjected();
    }

    private boolean multilineStyle;

    public StaticCell() {
        super();
    }

    @Override
    protected void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
        if (value != null) {
            if (multilineStyle) {
                sb.append(TEMPLATE.div(RESOURCES.style().bStaticCell__multiline_text(), value));
            } else {
                sb.append(TEMPLATE.div(RESOURCES.style().bStaticCell__text(), value));
            }
        }
    }

    public boolean isMultilineStyle() {
        return multilineStyle;
    }

    public void setMultilineStyle(boolean multilineStyle) {
        this.multilineStyle = multilineStyle;
    }
}
